//
//  MYDiscountCouponListVC.m
//  MuYing
//
//  Created by tbb on 2019/8/10.
//  Copyright © 2019 TBB. All rights reserved.
//

#import "MYDiscountCouponListVC.h"
#import "MYDiscountCouponModel.h"
@interface MYDiscountCouponListVC ()

@end

@implementation MYDiscountCouponListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    

}
-(void)setupView {
    [super setupView];
    self.listTableView.backgroundColor = kAPPTABLEVIEW_BG_COLOR;
    self.listTableView.frame = CGRectMake(0, 0, kYQPScreenWidth, kYQPScreenHeight-Height_NavBar-kHeight_Botton_SafeArea-44);
    kYQREGISTERTABLEVIEWNIBCELL(@"MYDiscountCouponListCell")
//    [self setupRefresh:self.listTableView];
    ws(self);
    self.listTableView.mj_header = [MJRefreshHeader headerWithRefreshingBlock:^{
        [weakself onStartRefresh];
    }];
    [self onStartRefresh];
    YQSHOWLOADINGTIPS(@"")
}

-(void)onStartRefresh {
    //self.page = 1;
    [self getUserCouponApi];
}

#pragma mark - 获取用户优惠券
- (void)getUserCouponApi
{
    
    [YQPNetWorkingManager sendGetRequest:kGET_QUERY_COUPON_URL parameters:@{@"useStatus":@(self.type)} success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        YQHIDELOADINGTIPS
        [self stopRefresh];
        if (responseObject.code == 0) {
            [self.dataArr removeAllObjects];
            NSArray *arr = [NSArray modelArrayWithClass:[MYDiscountCouponModel class] json:responseObject.data];
            [self.dataArr addObjectsFromArray:arr];
            [self.listTableView reloadData];
        }else{
            YQUITIPSERROR(responseObject.msg);
        }
    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        YQHIDELOADINGTIPS
        [self stopRefresh];
        YQUITIPSERROR(error);
    }];
}

#pragma mark - JXCategoryListContentViewDelegate

- (UIView *)listView {
    return self.view;
}

- (UIScrollView *)listScrollView {
    return self.listTableView;
}
#pragma mark - JXCategoryListContentViewDelegate end
-(void)withAction:(UIButton *)sender {
    NSInteger row = sender.tag - 11100;
    MYDiscountCouponModel *couponModel = nil;
    if (row < self.dataArr.count) {
        couponModel = self.dataArr[row];
    }
    
    YQSHOWLOADINGTIPS(@"");
    [YQPNetWorkingManager sendPostRequest:[NSString stringWithFormat:@"%@%@",kPOST_ACHIEVE_COUPON_URL,couponModel.couponId] parameters:nil success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        YQHIDELOADINGTIPS
        if (responseObject.code == 0) {
            [self getUserCouponApi];
        }else{
            YQUITIPSERROR(responseObject.msg)
        }
    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        YQHIDELOADINGTIPS
        YQUITIPSERROR(error)
    }];
    
}
#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    MYDiscountCouponListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYDiscountCouponListCell"];
    
    MYDiscountCouponModel *couponModel = nil;
    if (indexPath.row < self.dataArr.count) {
        couponModel = self.dataArr[indexPath.row];
    }
    [cell.discountsAmountLb rz_colorfulConfer:^(RZColorfulConferrer * _Nonnull confer) {
        confer.text(@"HKD ").font(kSystemMedium(15)).textColor(HexColor(0xFFFFFF));
        confer.text(couponModel.discountsAmount).font(kSystemMedium(32)).textColor(HexColor(0xFFFFFF));
    }];
//    cell.discountsAmountLb.text = [NSString stringWithFormat:@"HKD%@",couponModel.discountsAmount];
    cell.discountsAmountLb.textAlignment = NSTextAlignmentCenter;
    if (couponModel.minPoint > 0) {
        cell.minPointLb.text = [NSString stringWithFormat:@"%@HKD%.2lf%@",kYQLS(@"满"),couponModel.minPoint,kYQLS(@"可用")];;
    }else{
        cell.minPointLb.text = kYQLS(@"无门槛");
    }

    cell.titleLb.text = couponModel.name;
    cell.timeLb.text = [NSString stringWithFormat:@"%@",couponModel.endTime];
    
    if (self.type == 0) {
        cell.withBtn.ghostColor = kAPPThemeColor;
        [cell.withBtn setTitleColor:kAPPThemeColor forState:(UIControlStateNormal)];
        [cell.withBtn setTitle:kYQLS(@"立即使用") forState:(UIControlStateNormal)];
        cell.withBtn.tag = 11100 + indexPath.row;
        [cell.withBtn addTarget:self action:@selector(withAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }else if (self.type == 1) {
        cell.withBtn.ghostColor = HexColor(0xE1E1E1);
        [cell.withBtn setTitleColor:kAPPThreeTitleColor forState:(UIControlStateNormal)];
        cell.withBtn.enabled = NO;
        [cell.withBtn setTitle:kYQLS(@"已使用") forState:(UIControlStateNormal)];
    }else{
        cell.withBtn.ghostColor = HexColor(0xE1E1E1);
        [cell.withBtn setTitleColor:kAPPThreeTitleColor forState:(UIControlStateNormal)];
        cell.withBtn.enabled = NO;
        [cell.withBtn setTitle:kYQLS(@"已过期") forState:(UIControlStateNormal)];
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 122;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}
#pragma mark - UITableViewDelegate, UITableViewDataSource end
@end
